import 'dart:async';
import 'package:meta/meta.dart';
import 'package:sqflite/sqflite.dart';

import 'sqf_manager.dart';
///    author : leo
///    e-mail : jkliao@yeah.net
///    date   : 2020-03-2910:25
///    desc   :  数据库实例操作的基类
///    1、打开数据库
///    2、创建表（表的字段创建）：tableName和创建表的sql语句有具体子类去是实现
///
 abstract class BaseDbProvider {
  bool isTableExits = false;

  tableName();
  tableSqlString();

  ///table创建sql组装语句
  tableString(String tabName, String columnId) {
    return '''
    CREATE TABLE $tabName (
    $columnId INTEGER PRIMARY KEY AUTOINCREMENT,
    ''';
  }

  Future getDatabase() async => await open();

  @mustCallSuper
  prepare(tabName, sql) async {
    isTableExits = await SqfManager.isTableExits(tabName);
    if (!isTableExits) {
      Database database = await SqfManager.getCurrentDb();
      return await database.execute(sql);
    }
  }

  @mustCallSuper
  open() async {
    if (!isTableExits) {
      await prepare(tableName(), tableSqlString());
    }
    return await SqfManager.getCurrentDb();
  }


 }